package org.example.controller.post;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.annotation.HasRole;
import org.example.annotation.LogTrace;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.dto.post.PostDto;
import org.example.partten.builder.product.post.PPostDto;
import org.example.dao.entity.post.PImage;
import org.example.dao.entity.post.PPost;
import org.example.service.post.PostService;
import org.example.skytool.commom.ParamUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * POST(帖子层)
 *
 * @author 22449
 */
@RestController
@RequestMapping("/post")
@CrossOrigin
public class PostController {


    //TODO 功能

    /**
     * 转发帖子(用户)
     */
    @PostMapping("transferPost")
    public Result transferPost(@RequestBody PPost tPost) {
        Long id = tPost.getId();

        ParamUtil.LongParam(id);

        Long integer = postService.transferPost(id);
        return new Result(ResultCode.SUCCESS, integer);
    }

    /**
     * 点赞帖子(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("likePost")
    public Result likePost(@RequestBody PPost tPost) {
        Long id = tPost.getId();

        ParamUtil.LongParam(id);

        Long flag = postService.likePost(id);
        return new Result(ResultCode.SUCCESS, flag);
    }

    //TODO 新增

    /**
     * 发表帖子 上传图片文件(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("addPostByImage")
    public Result addPostByImage(@RequestBody PPost post) {

        Long themeId = post.getThemeId();
        String content = post.getContent();

        ParamUtil.LongParam(themeId);
        ParamUtil.StringParam(content);

        Long themeIdLong = Long.valueOf(themeId);

        PPostDto tpost1 = postService.addPostByImage(content, themeIdLong);
        return new Result(ResultCode.SUCCESS, tpost1);
    }

    /**
     * 上传帖子图片(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("addPostImage/{postId}")
    public Result addPostImage(@PathVariable("postId") Long postId, MultipartFile file) {
        ParamUtil.LongParam(postId);
        PImage tpost1 = postService.addPostImage(postId, file);
        return new Result(ResultCode.SUCCESS, tpost1);
    }

    //TODO 查询

    /**
     * 获取搜索的历史记录(用户)
     */
    @PostMapping("getSearchHistory")
    public Result getSearchHistory() {
        List<String> list = postService.getSearchHistory();
        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 清楚历史记录(用户)
     */
    @PostMapping("clearSearchHistory")
    public Result clearSearchHistory() {
        postService.clearSearchHistory();
        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 获取搜索推荐(用户)
     */
    @PostMapping("getSearchRecommend")
    public Result getSearchRecommend() {
        List<String> list = postService.getSearchRecommend();
        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 获取搜索热榜(用户)
     */
    @PostMapping("getSearchHotRank")
    public Result getSearchHotRank() {
        List<String> set = postService.getSearchHotRank();
        return new Result(ResultCode.SUCCESS, set);
    }

    /**
     * 获取搜索实时推荐(用户)
     */
    @PostMapping("getSearchIntime")
    public Result getSearchIntime(@RequestBody PPost post) {
        String content = post.getContent();

        ParamUtil.StringParam(content);

        List<String> list = postService.getSearchIntime(content);
        return new Result(ResultCode.SUCCESS, list);
    }



    /**
     * 获取热度帖子(用户)
     */
    @LogTrace
    @HasRole(role = "用户")
    @PostMapping("getPostByThemeAndHot/{limit}/{start}")
    public Result getPostByThemeAndHot(@RequestBody PPost tPost,
                                       @PathVariable("limit") Integer limit, @PathVariable("start") Integer start) {
        Long themeId = tPost.getThemeId();

        ParamUtil.LongParam(themeId);

        IPage<PostDto> list = postService.getPostByThemeAndHot(themeId, limit, start);
        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 搜索帖子列表(用户)
     */
    @LogTrace
    @HasRole(role = "用户")
    @PostMapping("searchPost/{limit}/{start}")
    public Result searchPost(@RequestBody PPost tPost,
                             @PathVariable("limit") Integer limit, @PathVariable("start") Integer start) {
        String content = tPost.getContent();
        Long themeId = tPost.getThemeId();
        Integer isValid = tPost.getIsValid();

        ParamUtil.StringParam(content);
        ParamUtil.LongParam(themeId);
        ParamUtil.IntegerParam(isValid);

        IPage<PostDto> list = postService.searchPost(isValid,themeId,content, limit, start);

        return new Result(ResultCode.SUCCESS, list);
    }


    /**
     * 获取帖子列表(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("getPostByTheme/{limit}/{start}")
    public Result getPostByTheme(@RequestBody PPost tPost,
                                 @PathVariable("limit") Integer limit, @PathVariable("start") Integer start) {
        Long themeId = tPost.getThemeId();

        ParamUtil.LongParam(themeId);

        IPage<PostDto> list = postService.getPostByTheme(themeId, limit, start);
        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 获取帖子详情(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("clickPost")
    public Result clickPost(@RequestBody PPost tPost) {
        Long id = tPost.getId();

        ParamUtil.LongParam(id);

        PostDto postDto = postService.clickPost(id);
        return new Result(ResultCode.SUCCESS, postDto);
    }

    /**
     * 获取加精帖子
     */
    @PostMapping("getPostByRecommend/{limit}/{start}")
    public Result getPostByRecommend(@RequestBody PPost tPost,
                                     @PathVariable("limit") Integer limit, @PathVariable("start") Integer start) {
        Long themeId = tPost.getThemeId();

        ParamUtil.LongParam(themeId);

        IPage<PostDto> list = postService.getPostByRecommend(themeId, limit, start);
        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 获取本人发布的帖子(用户)
     */
    @PostMapping("getPostByMine/{limit}/{start}")
    public Result getPostByMine(@PathVariable("limit") Integer limit, @PathVariable("start") Integer start) {
        IPage<PostDto> list = postService.getPostByMine(limit, start);
        return new Result(ResultCode.SUCCESS, list);
    }

    //TODO 更新

    /**
     * 修改帖子(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("updatePost")
    public Result updatePost(@RequestBody PPost tPost) {
        String content = tPost.getContent();
        Long themeId = tPost.getThemeId();
        Long id = tPost.getId();

        ParamUtil.LongParam(themeId, id);
        ParamUtil.StringParam(content);

        PPost tpost1 = postService.updatePost(id, content, themeId);
        return new Result(ResultCode.SUCCESS, tpost1);
    }


    //TODO 删除

    /**
     * 删除帖子(用户)
     */
    @PostMapping("deletePost")
    public Result deletePost(@RequestBody PPost tPost) {
        Long id = tPost.getId();

        ParamUtil.LongParam(id);

        postService.deletePost(id);
        return new Result(ResultCode.SUCCESS, null);
    }


    @Resource
    private PostService postService;


}
